/*
 * Copyright (C) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "imageknife_trace.h"
#include <hitrace/trace.h>


namespace ImageKnifePro {

void ImageKnifeTrace::StartSyncTrace()
{
    if (started_) {
        return;
    }
    started_ = true;
    syncTrace_ = true;

    traceName_ = "IMAGE_KNIFE_PRO " + traceName_;
    OH_HiTrace_StartTrace(traceName_.c_str());
}

ImageKnifeTrace::ImageKnifeTrace(std::string info) : traceName_(info)
{
}

ImageKnifeTrace::ImageKnifeTrace(std::string info, std::string componentId)
    : ImageKnifeTrace(info)
{
    traceName_ += " [ID:" + componentId + "]";
}

ImageKnifeTrace::ImageKnifeTrace(std::string info, std::shared_ptr<ImageKnifeTaskInternal> task)
    : ImageKnifeTrace(info)
{
    IMAGE_KNIFE_CHECK(task == nullptr);
    traceName_ += " " + task->GetTaskInfo();
}

ImageKnifeTrace::ImageKnifeTrace(std::string info, std::shared_ptr<ImageKnifeRequestInternal> request)
    : ImageKnifeTrace(info, request != nullptr ? request->GetNodeId() : "Null")
{
}

ImageKnifeTrace::ImageKnifeTrace(std::string info, std::shared_ptr<ImageKnifeRequestInternal> request,
    ImageRequestType type) : ImageKnifeTrace(info)
{
    traceName_ = ImageKnifeTaskInternal::GetTaskInfo(type, request) + traceName_;
}

void ImageKnifeTrace::FinishTrace()
{
    if (!started_) {
        return;
    }
    started_ = false;

    if (syncTrace_) {
        OH_HiTrace_FinishTrace();
    }
}

ImageKnifeTrace::~ImageKnifeTrace()
{
    FinishTrace();
}
}

